/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package depena.urbarama.web;

import depena.urbarama.data.Database;
import depena.urbarama.data.Project;
import depena.urbarama.data.UProjectList;
import javax.ws.rs.*;

/**
 *
 * @author Alberto
 */
@Path("projects")
public class ProjectResource 
{
    private final String _url = "jdbc:mysql://taz.harding.edu/jdepena?"
            + "user=jdepena&password=jdepena";
    
    private Database _db;
    
    public ProjectResource()
    {
        try { 
            Class.forName("com.mysql.jdbc.Driver").newInstance(); 
            _db = new Database(_url);
        } 
        catch (Exception ex) { System.out.print("Error" + ex); }
    }
    
    @GET
    @Path("/json")
    @Produces("application/json")
    public UProjectList getJSONProjects(@QueryParam("owner") String owner,
        @QueryParam("title") String title) 
    {
        if (owner != null)
        {
            return getProjectListByOwner(owner);
        }
        else if (owner == null && title != null)
        {
            return getProjectListByTitle(title);
        }
        else
        {
            return getProjectList();
        }
    }
    
    @GET
    @Path("/xml")
    @Produces("application/xml")
    public UProjectList getXMLProjects(@QueryParam("owner") String owner,
        @QueryParam("title") String title)  
    {
        if (owner != null)
        {
            return getProjectListByOwner(owner);
        }
        else if (owner == null && title != null)
        {
            return getProjectListByTitle(title);
        }
        else
        {
            return getProjectList();
        }
    }
    
    public UProjectList getProjectList()
    {
        try 
        {
            UProjectList list = _db.getProjects();
            
            return list;
        } 
        catch (Exception ex) 
        {
            System.out.println("Exception: " + ex);
            return null;
        }
    }
    
    public UProjectList getProjectListByOwner(String owner)
    {
        try 
        {
            UProjectList list = _db.getProjectsByOwner(owner);
            
            return list;
        } 
        catch (Exception ex) 
        {
            System.out.println("Exception: " + ex);
            return null;
        }
    }
    
    public UProjectList getProjectListByTitle(String title)
    {
        try 
        {
            UProjectList list = _db.getProjectsByTitle(title);
            
            return list;
        } 
        catch (Exception ex) 
        {
            System.out.println("Exception: " + ex);
            return null;
        }
    }
    
    @PUT
    @Consumes({"application/xml", "application/json"})
    public void createProject(Project project) 
    {
       System.out.println("createProject: " + project);
       try 
        {
            _db.insertProject(project);
        } 
        catch (Exception ex) 
        {
            System.out.println("Exception: " + ex);
        }
    }

}
